//
//  DYColorController.m
//  colorTest
//
//  Created by 丁益 on 2017/4/2.
//  Copyright © 2017年 celi. All rights reserved.
//  彩色模式

#import "DYColorController.h"
#import "UIColor-Expanded.h"
#import "DYColorView.h"
#import "DYSelectColorView.h"
#import "MBProgressHUD+MJ.h"
#import "AsyncUdpSocket.h"
#import "SendWifiDataTool.h"

@interface DYColorController ()<DYSelectColorViewDelegate>
{
    SendWifiDataTool *_sendSingleColor;
}
@property (nonatomic, weak) DYColorView *colorView;
@property (nonatomic, weak) KZColorPicker *picker;
@end


@implementation DYColorController

- (UIColor *)selectedColor
{
    if (_selectedColor == nil) {
        _selectedColor = [UIColor redColor];
    }
    
    return _selectedColor;
}

- (void)viewDidLoad {
    
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.title = @"彩色";
    KZColorPicker *picker = [[KZColorPicker alloc] initWithFrame: CGRectMake(0, 64, sWidth, sHeight)];
    picker.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    picker.selectedColor = self.selectedColor;
    picker.oldColor = self.selectedColor;
    picker.isShowAlphaSlider = NO;//项目中不需要对涂明渡设置
    [picker addTarget:self action:@selector(pickerChanged:) forControlEvents:UIControlEventValueChanged];
    self.picker = picker;
    [self.view addSubview:picker];
    
    DYColorView *colorView = [DYColorView colorViewWithFream:CGRectMake(8, 114, 45, 60) RedValue:[NSString stringWithFormat:@"%0.0f",self.selectedColor.red*255]  blueValue:[NSString stringWithFormat:@"%0.0f",self.selectedColor.blue*255]  greenValue:[NSString stringWithFormat:@"%0.0f",self.selectedColor.green*255] addTarget:self action:@selector(labelOnclick:)];
    self.colorView = colorView;
    [self.view addSubview:colorView];
    
    
    
    UIButton *pwrBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    
    [pwrBtn addTarget:self action:@selector(onClickedPwrbtn:) forControlEvents:UIControlEventTouchUpInside];
    [pwrBtn setBackgroundImage:[UIImage imageNamed:@"icon_power"] forState:UIControlStateNormal];
    [pwrBtn setBackgroundImage:[UIImage imageNamed:@"icon_poweroff"] forState:UIControlStateSelected];
    
    UIBarButtonItem *pwrBarItem = [[UIBarButtonItem alloc]initWithCustomView:pwrBtn];
    
    self.navigationItem.rightBarButtonItem = pwrBarItem;
    
    SendWifiDataTool *sendSingleColor = [SendWifiDataTool sharedSendWifiDataTool];
    _sendSingleColor = sendSingleColor;
    
    DLog("come baby");
}


- (void)onClickedPwrbtn:(UIButton *)btn {
    NSLog(@"onClickedPwrbtn");
    
    DLog(@"btn.selected = %d",btn.selected);
    btn.selected = !btn.selected;
    if (btn.selected) {
        [_sendSingleColor sendPowerCmd:closeDevice];
    } else {
        [_sendSingleColor sendPowerCmd:openDevice];
    }
    
    
}

- (void) pickerChanged:(KZColorPicker *)cp
{
    
    NSLog(@"R=%f-G=%f-B=%f-l=%f",cp.selectedColor.red,cp.selectedColor.green,cp.selectedColor.blue,cp.BrightnessValue);
    
    NSString *redStringValue = [NSString stringWithFormat:@"%0.0f",cp.selectedColor.red*255];
    NSString *greenStringValue = [NSString stringWithFormat:@"%0.0f",cp.selectedColor.green*255];
    NSString *blueStringValue = [NSString stringWithFormat:@"%0.0f",cp.selectedColor.blue*255];
    
    
    [self.colorView setRedValue:redStringValue greenValue: greenStringValue blueValue:blueStringValue];
    self.selectedColor = cp.selectedColor;
    
 
    [_sendSingleColor sendSingleRGBColorCmdRedValue:redStringValue blueValue:blueStringValue greenValue:greenStringValue];
}

- (void)labelOnclick:(UIButton *)btn
{
    DYSelectColorView *selectColorView = [[DYSelectColorView alloc]initWithSelectColorWithRedValue:[NSString stringWithFormat:@"%0.0f",self.selectedColor.red*255] blueValue:[NSString stringWithFormat:@"%0.0f",self.selectedColor.blue*255] greenValue:[NSString stringWithFormat:@"%0.0f",self.selectedColor.green*255]];
    selectColorView.delegate = self;
    [selectColorView showColorView];
    [self.view addSubview:selectColorView];
}

#pragma DYSelectColorViewDelegate
- (void)selectColorWithRedValue:(NSString *)redValue blueValue:(NSString *)blueValue greenValue:(NSString *)greenValue
{
    [self.colorView setRedValue:redValue greenValue:greenValue blueValue:blueValue];
    self.selectedColor = DYColor([redValue integerValue], [greenValue integerValue], [blueValue integerValue]);
    
    [self.picker setSelectedColor:self.selectedColor animated:YES];
}

@end
